function varargout = Settings_GUI(varargin)
% SETTINGS_GUI MATLAB code for Settings_GUI.fig
%      SETTINGS_GUI, by itself, creates a new SETTINGS_GUI or raises the existing
%      singleton*.
%
%      H = SETTINGS_GUI returns the handle to a new SETTINGS_GUI or the handle to
%      the existing singleton*.
%
%      SETTINGS_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SETTINGS_GUI.M with the given input arguments.
%
%      SETTINGS_GUI('Property','Value',...) creates a new SETTINGS_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Settings_GUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Settings_GUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Settings_GUI

% Last Modified by GUIDE v2.5 27-Jul-2012 02:03:25

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Settings_GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @Settings_GUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Settings_GUI is made visible.
function Settings_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Settings_GUI (see VARARGIN)

% Choose default command line output for Settings_GUI
handles.output = hObject;
% set Popup <-> address dictionary
handles.address_values = {...
    'handles.uControllerAddressPopup','handles.settings.uC_address';...
    'handles.GPIBControllerPopup','handles.settings.GPIB_controller';...
    'handles.GPIBAddressPopup','handles.settings.GPIB_address';...    
    'handles.AnalyzerAddressPopup','handles.settings.AN_address';...
    'handles.LOGeneratorAddressPopup','handles.settings.LO_address';...
    'handles.RFGeneratorAddressPopup','handles.settings.RF_address';...
    'handles.DCSupplyAddressPopup','handles.settings.DC_address'...
    };
% set CheckBox <-> setting dictionary
handles.boolean_values = {...
    'handles.LODetectionEnableCheckbox', 'handles.settings.meas_LO_detection';...
    'handles.ReflectivityEnableCheckbox', 'handles.settings.meas_reflectivity';...
    'handles.NonlinearEnableCheckbox','handles.settings.meas_nonlinear';...
    'handles.Ch2ChCrosstalksEnableCheckbox','handles.settings.meas_ch2ch_crosstalks';...
    'handles.Clk2ChCrosstalksEnableCheckbox','handles.settings.meas_clk2ch_crosstalks';...
    'handles.TemperatureEnableCheckbox','handles.settings.meas_temperature';...
    'handles.ChannelGainEnableCheckbox','handles.settings.meas_gain'...
    };
% set TextEdit <-> setting dictionary
handles.numerical_values = {...
    'handles.VoltageEdit','handles.settings.DC_voltage';...
    'handles.CurrentLimitEdit', 'handles.settings.DC_current_limit';...
	'handles.MinCurrentEdit', 'handles.settings.eval_current_min';...
    'handles.MaxCurrentEdit', 'handles.settings.eval_current_max';...
    'handles.LOPowerSweepStepEdit', 'handles.settings.LO_power_step';...
    'handles.LOPowerSweepMaxEdit', 'handles.settings.LO_power_max';...
    'handles.LOPowerSweepMinEdit', 'handles.settings.LO_power_min';...
    'handles.MaxLODetectionUpperLimitEdit','handles.settings.eval_LO_detection_max_max';...
    'handles.MinLODetectionUpperLimitEdit','handles.settings.eval_LO_detection_max_min';...
    'handles.MaxLODetectionLowerLimitEdit','handles.settings.eval_LO_detection_min_min';...
    'handles.MinLODetectionUpperLimitEdit','handles.settings.eval_LO_detection_min_max';...
    'handles.MaxRFInputReflectivityEdit', 'handles.settings.eval_reflectivity_RF_max';...
	'handles.MaxLOInputReflectivityEdit','handles.settings.eval_reflectivity_LO_max';...
    'handles.MinOP1dBEdit','handles.settings.eval_P1dB_min';...
    'handles.RFPowerSweepStepEdit','handles.settings.RF_power_step';...
    'handles.RFPowerSweepMaxEdit','handles.settings.RF_power_max';...
    'handles.RFPowerSweepMinEdit','handles.settings.RF_power_min';...
    'handles.MaxCh2ChCrosstalksEdit','handles.settings.eval_ch2ch_crosstalks_max';...
    'handles.MaxClk2ChCrosstalksEdit','handles.settings.eval_clk2ch_crosstalks_max';...
    'handles.MaxTemperatureEdit','handles.settings.eval_temperature_max';...
    'handles.RFPowerEdit','handles.settings.RF_power_value';...
    'handles.LOPowerEdit','handles.settings.LO_power_value';...
    'handles.MinGainEdit','handles.settings.eval_gain_min'...
    };
handles.settings = varargin{3};
guidata(hObject, handles);

load_settings(hObject, handles);

% Update handles structure

% UIWAIT makes Settings_GUI wait for user response (see UIRESUME)
uiwait(hObject);

function Settings_GUI_CloseRequestFcn(hObject,eventdata,handles)
uiresume(hObject);

% --- Outputs from this function are returned to the command line.
function varargout = Settings_GUI_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Return the settings object and commit suicide
varargout{1} = handles.output;
delete(hObject);

% --- Executes on button press in LODetectionEnableCheckbox.
function LODetectionEnableCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to LODetectionEnableCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
    enabled = 'on';
else
    enabled = 'off';
end
set(get(handles.LODetectionSettingsPanel,'children'),{'enable'},{enabled});
set(get(handles.LODetectionEvaluationPanel,'children'),{'enable'},{enabled});

% --- Executes on selection change in uControllerAddressPopup.
function uControllerAddressPopup_Callback(hObject, eventdata, handles)
% hObject    handle to uControllerAddressPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns uControllerAddressPopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from uControllerAddressPopup


% --- Executes during object creation, after setting all properties.
function uControllerAddressPopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uControllerAddressPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in AnalyzerAddressPopup.
function AnalyzerAddressPopup_Callback(hObject, eventdata, handles)
% hObject    handle to AnalyzerAddressPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns AnalyzerAddressPopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from AnalyzerAddressPopup


% --- Executes during object creation, after setting all properties.
function AnalyzerAddressPopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to AnalyzerAddressPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in LOGeneratorAddressPopup.
function LOGeneratorAddressPopup_Callback(hObject, eventdata, handles)
% hObject    handle to LOGeneratorAddressPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns LOGeneratorAddressPopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from LOGeneratorAddressPopup


% --- Executes during object creation, after setting all properties.
function LOGeneratorAddressPopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LOGeneratorAddressPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in RFGeneratorAddressPopup.
function RFGeneratorAddressPopup_Callback(hObject, eventdata, handles)
% hObject    handle to RFGeneratorAddressPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns RFGeneratorAddressPopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from RFGeneratorAddressPopup


% --- Executes during object creation, after setting all properties.
function RFGeneratorAddressPopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RFGeneratorAddressPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in DCSupplyAddressPopup.
function DCSupplyAddressPopup_Callback(hObject, eventdata, handles)
% hObject    handle to DCSupplyAddressPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns DCSupplyAddressPopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from DCSupplyAddressPopup


% --- Executes during object creation, after setting all properties.
function DCSupplyAddressPopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to DCSupplyAddressPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in GPIBAddressPopup.
function GPIBAddressPopup_Callback(hObject, eventdata, handles)
% hObject    handle to GPIBAddressPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns GPIBAddressPopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from GPIBAddressPopup


% --- Executes during object creation, after setting all properties.
function GPIBAddressPopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to GPIBAddressPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in GPIBControllerPopup.
function GPIBControllerPopup_Callback(hObject, eventdata, handles)
% hObject    handle to GPIBControllerPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns GPIBControllerPopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from GPIBControllerPopup


% --- Executes during object creation, after setting all properties.
function GPIBControllerPopup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to GPIBControllerPopup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in NonlinearEnableCheckbox.
function NonlinearEnableCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to NonlinearEnableCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
    enabled = 'on';
else
    enabled = 'off';
end
set(get(handles.NonlinearSettingsPanel,'children'),{'enable'},{enabled});
set(get(handles.NonlinearEvaluationPanel,'children'),{'enable'},{enabled});

% --- Executes on button press in ReflectivityEnableCheckbox.
function ReflectivityEnableCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to ReflectivityEnableCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
    enabled = 'on';
else
    enabled = 'off';
end
set(get(handles.ReflectivityEvaluationPanel,'children'),{'enable'},{enabled});

% --- Executes on button press in ChannelGainEnableCheckbox.
function ChannelGainEnableCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to ChannelGainEnableCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
    enabled = 'on';
else
    enabled = 'off';
end
set(get(handles.ChannelGainSettingsPanel,'children'),{'enable'},{enabled});
set(get(handles.ChannelGainEvaluationPanel,'children'),{'enable'},{enabled});

% --- Executes on button press in Clk2ChCrosstalksEnableCheckbox.
function Clk2ChCrosstalksEnableCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to Clk2ChCrosstalksEnableCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
    enabled = 'on';
else
    enabled = 'off';
end
set(get(handles.Clk2ChCrosstalksEvaluationPanel,'children'),{'enable'},{enabled});

% --- Executes on button press in Ch2ChCrosstalksEnableCheckbox.
function Ch2ChCrosstalksEnableCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to Ch2ChCrosstalksEnableCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
    enabled = 'on';
else
    enabled = 'off';
end
set(get(handles.Ch2ChCrosstalksEvaluationPanel,'children'),{'enable'},{enabled});


% --- Executes on button press in ApplyButton.
function ApplyButton_Callback(hObject, eventdata, handles)
% hObject    handle to ApplyButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% clearing errors
error = apply_settings(hObject, handles);

if isempty(error)
    guidata(hObject,handles);
    uiresume(handles.SettingsFigure);
else
    h = msgbox('Please correct the values marked in red.','Error','error');
    uiwait(h);
end

% --- Executes on button press in TemperatureEnableCheckbox.
function TemperatureEnableCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to TemperatureEnableCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(hObject,'Value')
    enabled = 'on';
else
    enabled = 'off';
end
set(get(handles.TemperatureEvaluationPanel,'children'),{'enable'},{enabled});


% --- Executes on button press in CancelButton.
function CancelButton_Callback(hObject, eventdata, handles)
% hObject    handle to CancelButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
uiresume(handles.SettingsFigure);

% --- Executes on button press in RestoreDefaultButton.
function RestoreDefaultButton_Callback(hObject, eventdata, handles)
% hObject    handle to RestoreDefaultButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
path = fullfile(pwd, Settings.DEFAULT_DIR, Settings.DEFAULT_FILE);
% load default settings
handles.settings = Settings(path);
guidata(hObject,handles);
% apply changes to the figure
load_settings(hObject, handles);

% --- Executes on button press in SaveButton.
function SaveButton_Callback(hObject, eventdata, handles)
% hObject    handle to SaveButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
error = apply_settings(hObject, handles);

if isempty(error)
    guidata(hObject,handles);
    [filename, pathname] = uiputfile( ...
        {'*.set','Settings Files (*.set)'; '*.*',  'All Files (*.*)'} ...
        ,'Save settings as');
    if ~isequal(filename,0) && ~isequal(pathname,0)
       settings = handles.settings;
       save(fullfile(pathname,filename),'settings');
    end
else
    h = msgbox('Please correct the values marked in red.','Error','error');
    uiwait(h);
end


% --- Executes on button press in LoadButton.
function LoadButton_Callback(hObject, eventdata, handles)
% hObject    handle to LoadButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile( ...
    {'*.set','Settings Files (*.set)'; '*.*',  'All Files (*.*)'} ...
    ,'Open settings file');
if ~isequal(filename,0) && ~isequal(pathname,0)
    handles.settings = Settings(fullfile(pathname,filename));
    guidata(hObject,handles);
    % apply changes to the figure
    load_settings(hObject, handles);
end


function MinCurrentEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MinCurrentEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MinCurrentEdit as text
%        str2double(get(hObject,'String')) returns contents of MinCurrentEdit as a double


% --- Executes during object creation, after setting all properties.
function MinCurrentEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MinCurrentEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function MaxCurrentEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MaxCurrentEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MaxCurrentEdit as text
%        str2double(get(hObject,'String')) returns contents of MaxCurrentEdit as a double


% --- Executes during object creation, after setting all properties.
function MaxCurrentEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxCurrentEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function VoltageEdit_Callback(hObject, eventdata, handles)
% hObject    handle to VoltageEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of VoltageEdit as text
%        str2double(get(hObject,'String')) returns contents of VoltageEdit as a double


% --- Executes during object creation, after setting all properties.
function VoltageEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to VoltageEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function CurrentLimitEdit_Callback(hObject, eventdata, handles)
% hObject    handle to CurrentLimitEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of CurrentLimitEdit as text
%        str2double(get(hObject,'String')) returns contents of CurrentLimitEdit as a double


% --- Executes during object creation, after setting all properties.
function CurrentLimitEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CurrentLimitEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function MaxLODetectionUpperLimitEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MaxLODetectionUpperLimitEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MaxLODetectionUpperLimitEdit as text
%        str2double(get(hObject,'String')) returns contents of MaxLODetectionUpperLimitEdit as a double


% --- Executes during object creation, after setting all properties.
function MaxLODetectionUpperLimitEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxLODetectionUpperLimitEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function MinLODetectionUpperLimitEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MinLODetectionUpperLimitEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MinLODetectionUpperLimitEdit as text
%        str2double(get(hObject,'String')) returns contents of MinLODetectionUpperLimitEdit as a double


% --- Executes during object creation, after setting all properties.
function MinLODetectionUpperLimitEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MinLODetectionUpperLimitEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function MaxLODetectionLowerLimitEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MaxLODetectionLowerLimitEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MaxLODetectionLowerLimitEdit as text
%        str2double(get(hObject,'String')) returns contents of MaxLODetectionLowerLimitEdit as a double


% --- Executes during object creation, after setting all properties.
function MaxLODetectionLowerLimitEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxLODetectionLowerLimitEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function MinLODetectionLowerLimitEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MinLODetectionLowerLimitEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MinLODetectionLowerLimitEdit as text
%        str2double(get(hObject,'String')) returns contents of MinLODetectionLowerLimitEdit as a double


% --- Executes during object creation, after setting all properties.
function MinLODetectionLowerLimitEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MinLODetectionLowerLimitEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function LOPowerSweepStepEdit_Callback(hObject, eventdata, handles)
% hObject    handle to LOPowerSweepStepEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of LOPowerSweepStepEdit as text
%        str2double(get(hObject,'String')) returns contents of LOPowerSweepStepEdit as a double


% --- Executes during object creation, after setting all properties.
function LOPowerSweepStepEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LOPowerSweepStepEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function LOPowerSweepMaxEdit_Callback(hObject, eventdata, handles)
% hObject    handle to LOPowerSweepMaxEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of LOPowerSweepMaxEdit as text
%        str2double(get(hObject,'String')) returns contents of LOPowerSweepMaxEdit as a double


% --- Executes during object creation, after setting all properties.
function LOPowerSweepMaxEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LOPowerSweepMaxEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function LOPowerSweepMinEdit_Callback(hObject, eventdata, handles)
% hObject    handle to LOPowerSweepMinEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of LOPowerSweepMinEdit as text
%        str2double(get(hObject,'String')) returns contents of LOPowerSweepMinEdit as a double


% --- Executes during object creation, after setting all properties.
function LOPowerSweepMinEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LOPowerSweepMinEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function MaxRFInputReflectivityEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MaxRFInputReflectivityEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MaxRFInputReflectivityEdit as text
%        str2double(get(hObject,'String')) returns contents of MaxRFInputReflectivityEdit as a double


% --- Executes during object creation, after setting all properties.
function MaxRFInputReflectivityEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxRFInputReflectivityEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function MaxLOInputReflectivityEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MaxLOInputReflectivityEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MaxLOInputReflectivityEdit as text
%        str2double(get(hObject,'String')) returns contents of MaxLOInputReflectivityEdit as a double


% --- Executes during object creation, after setting all properties.
function MaxLOInputReflectivityEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxLOInputReflectivityEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function RFPowerSweepStepEdit_Callback(hObject, eventdata, handles)
% hObject    handle to RFPowerSweepStepEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of RFPowerSweepStepEdit as text
%        str2double(get(hObject,'String')) returns contents of RFPowerSweepStepEdit as a double


% --- Executes during object creation, after setting all properties.
function RFPowerSweepStepEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RFPowerSweepStepEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function RFPowerSweepMaxEdit_Callback(hObject, eventdata, handles)
% hObject    handle to RFPowerSweepMaxEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of RFPowerSweepMaxEdit as text
%        str2double(get(hObject,'String')) returns contents of RFPowerSweepMaxEdit as a double


% --- Executes during object creation, after setting all properties.
function RFPowerSweepMaxEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RFPowerSweepMaxEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function RFPowerSweepMinEdit_Callback(hObject, eventdata, handles)
% hObject    handle to RFPowerSweepMinEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of RFPowerSweepMinEdit as text
%        str2double(get(hObject,'String')) returns contents of RFPowerSweepMinEdit as a double


% --- Executes during object creation, after setting all properties.
function RFPowerSweepMinEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RFPowerSweepMinEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function MinOP1dBEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MinOP1dBEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MinOP1dBEdit as text
%        str2double(get(hObject,'String')) returns contents of MinOP1dBEdit as a double


% --- Executes during object creation, after setting all properties.
function MinOP1dBEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MinOP1dBEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function MaxCh2ChCrosstalksEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MaxCh2ChCrosstalksEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MaxCh2ChCrosstalksEdit as text
%        str2double(get(hObject,'String')) returns contents of MaxCh2ChCrosstalksEdit as a double


% --- Executes during object creation, after setting all properties.
function MaxCh2ChCrosstalksEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxCh2ChCrosstalksEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function MaxClk2ChCrosstalksEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MaxClk2ChCrosstalksEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MaxClk2ChCrosstalksEdit as text
%        str2double(get(hObject,'String')) returns contents of MaxClk2ChCrosstalksEdit as a double


% --- Executes during object creation, after setting all properties.
function MaxClk2ChCrosstalksEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxClk2ChCrosstalksEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function MaxTemperatureEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MaxTemperatureEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MaxTemperatureEdit as text
%        str2double(get(hObject,'String')) returns contents of MaxTemperatureEdit as a double


% --- Executes during object creation, after setting all properties.
function MaxTemperatureEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxTemperatureEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function MinGainEdit_Callback(hObject, eventdata, handles)
% hObject    handle to MinGainEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MinGainEdit as text
%        str2double(get(hObject,'String')) returns contents of MinGainEdit as a double


% --- Executes during object creation, after setting all properties.
function MinGainEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MinGainEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function RFPowerEdit_Callback(hObject, eventdata, handles)
% hObject    handle to RFPowerEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of RFPowerEdit as text
%        str2double(get(hObject,'String')) returns contents of RFPowerEdit as a double


% --- Executes during object creation, after setting all properties.
function RFPowerEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RFPowerEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function LOPowerEdit_Callback(hObject, eventdata, handles)
% hObject    handle to LOPowerEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of LOPowerEdit as text
%        str2double(get(hObject,'String')) returns contents of LOPowerEdit as a double


% --- Executes during object creation, after setting all properties.
function LOPowerEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LOPowerEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes when user attempts to close SettingsFigure.
function SettingsFigure_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to SettingsFigure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
uiresume(hObject);

% --- Executes when the settings are loaded from the file
function load_settings(hObject, handles)
% setting numerical values
for i = 1:length(handles.numerical_values)
    set(eval(handles.numerical_values{i,1}),'String'...
        , eval(handles.numerical_values{i,2}));
end
% setting boolean values
for i = 1:length(handles.boolean_values)
    set(eval(handles.boolean_values{i,1}),'Value'...
        , eval(handles.boolean_values{i,2}));
    eval([regexprep(handles.boolean_values{i,1},'handles\.','')...
        '_Callback(' handles.boolean_values{i,1} ', [], handles)']);
end
% setting GPIB controller field
gpibdev = instrhwinfo('GPIB');
value = find(strcmp(gpibdev.InstalledAdaptors,handles.settings.GPIB_controller));
if isempty(value)
    value = 1;
end
set(handles.GPIBControllerPopup,'String',gpibdev.InstalledAdaptors);
set(handles.GPIBControllerPopup,'Value',value);
% setting COM address field
serialdev = instrhwinfo('serial');
value = find(strcmp(serialdev.SerialPorts,handles.settings.uC_address));
if isempty(value)
    value = 1;
end
set(handles.uControllerAddressPopup,'String',serialdev.SerialPorts);
set(handles.uControllerAddressPopup,'Value',value);
% setting other gpib addresses
gpibaddresses = num2cell(handles.settings.GPIB_ADDRESS_MIN...
    :handles.settings.GPIB_ADDRESS_MAX);
for i = 3:length(handles.address_values)
    set(eval(handles.address_values{i,1}),'String',gpibaddresses);
    value = 1-handles.settings.GPIB_ADDRESS_MIN+eval(handles.address_values{i,2});
    set(eval(handles.address_values{i,1}),'Value',value);
end


function error = apply_settings(hObject, handles)
error = [];
fields = [findobj(handles.SettingsFigure,'Style','edit');...
     findobj(handles.SettingsFigure,'Style','popupmenu')];
set(fields,{'BackgroundColor'},{[1 1 1]});
set(fields,{'TooltipString'},{''});

for i = 1:length(handles.numerical_values)
    try
        eval([handles.numerical_values{i,2} ' = str2double(get(' ...
            handles.numerical_values{i,1} ',''String''));']);
    catch error
        set(eval(handles.numerical_values{i,1}),'BackgroundColor',[1 0 0]);
        set(eval(handles.numerical_values{i,1}),'TooltipString',error.message);
    end
end

for i = 1:length(handles.boolean_values)
    eval([handles.boolean_values{i,2} ' = get(' ...
            handles.boolean_values{i,1} ',''Value'');']);
end

for i = 1:2
    value = get(eval(handles.address_values{i,1}),'Value');
    disp(value);
    contents = cellstr(get(eval(handles.address_values{i,1}),'String'));
    disp(contents);
    eval([handles.address_values{i,2} '= contents{value};']);
end

for i = 3:length(handles.address_values)
    value = get(eval(handles.address_values{i,1}),'Value');
    contents = cellstr(get(eval(handles.address_values{i,1}),'String'));
    eval([handles.address_values{i,2} '= str2double(contents{value});']);
end
